<template>
  <div class="app-container">
    <el-card class="box-card" shadow="never">
      <el-form :inline="true" :model="dataForm">
        <!-- <el-form-item label="公司名称">
          <el-select v-model="dataForm.companyName" placeholder="请选择">
            <el-option
              v-for="item in companyList"
              :key="item.id"
              :label="item.companyName"
              :value="item.companyName"
            ></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item label="登录账号">
          <el-input v-model="dataForm.userName" clearable placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card" shadow="never">
      <div slot="header">
        <el-button type="primary" @click="handleEdit('new')">新增</el-button>
      </div>
      <!-- 数据展示 -->
      <el-table
        :data="tableData"
        :header-cell-style="{background:'#f0f3fa',textAlign:'center'}"
        style="width: 100%"
        :row-class-name="tableRowClassName"
        :cell-style="{textAlign:'center'}"
      >
        <el-table-column prop="name" label="用户名"></el-table-column>
        <el-table-column prop="userName" label="登录账号"></el-table-column>
        <!-- <el-table-column prop="companyName" label="企业"></el-table-column> -->
        <el-table-column prop="roleName" label="账号类型"></el-table-column>
        <el-table-column prop="createDate" label="创建日期"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope" v-if="scope.row.id !== 1">
            <el-switch
              v-model="scope.row.enabled"
              :active-value="1"
              :inactive-value="0"
              @change="handleChangeSwitch(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope" v-if="scope.row.id !== 1">
            <el-tooltip class="item" content="修改密码">
              <i class="el-icon-unlock" @click="handleEdit('edit', scope.row)"></i>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="dataForm.page"
        :page-sizes="[10, 20, 40, 60]"
        :page-size="dataForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 创建角色 -->
    <newRole ref="newRole" @updata="getAllUserReq"></newRole>
  </div>
</template>

<script>
import { getAllCompany } from '@/api/base'
import { getAllUser, updateEnabled } from '@/api/other'
import newRole from './component/newRole.vue'
export default {
  // 组件注册
  components: {
    newRole,
  },
  data() {
    return {
      dataForm: {
        companyName: '',
        userName: '',
        page: 1,
        pageSize: 10,
      },
      companyList: [],
      tableData: [],
      total: 400,
    }
  },
  mounted() {
    this.getAllCompanyReq() // 所有公司
    this.getAllUserReq()
  },
  methods: {
    getAllCompanyReq() {
      getAllCompany({ page: 1, pageSize: 100 }).then((res) => {
        const { code, data, msg } = res
        if (code !== '0000') return this.$message.error(msg)
        this.companyList = data.list
      })
    },
    getAllUserReq() {
      getAllUser(this.dataForm).then((res) => {
        const { code, data, msg } = res
        if (code !== '0000') return this.$message.error(msg)
        const { total, list } = data
        this.total = total
        this.tableData = list
      })
    },
    handleChangeSwitch({ id, enabled }) {
      updateEnabled({ id, enabled }).then((res) => {
        const { code, msg } = res
        if (code !== '0000') return this.$message.error(msg)
        this.getAllUserReq()
      })
    },
    onSubmit() {
      this.dataForm.page = 1
      this.getAllUserReq()
    },
    handleEdit(type, item) {
      this.$nextTick((_) => {
        this.$refs.newRole.init(type, item)
      })
    },
    // 监听pageSize改变事件
    handleSizeChange(newSize) {
      this.dataForm.pageSize = newSize
      this.getAllUserReq()
    },
    // 监听 页面值 改变的事件
    handleCurrentChange(newPage) {
      this.dataForm.page = newPage
      this.getAllUserReq()
    },
  },
}
</script>

<style lang='scss' scoped>
</style>
